import { localCache } from '@/utils/cache'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/main',
    },
    {
      path: '/login',
      component: () => import('../views/login/Login.vue'),
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue'),
    },
    {
      path: '/:pathMatch(.*)',
      component: () => import('../views/not-fount/NotFound.vue'),
    },
  ],
})

// 导航守卫
// 参数 to 跳转到的位置  from 从哪里跳转过来
router.beforeEach((to, from) => {
  if (to.path === '/main') {
    const token = localCache.getCache('token')
    if (!token) return '/login'
  }
})

export default router
